<template>
  <div style="height: 95%;">
      <el-table :data="showTable" border style="width: 100%"
      v-loading="Batchloading"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      :header-cell-style="{background:'#eef1f6',color:'#606266','text-align':'center'}"
      stripe
      :row-key="getRowKeys"
      :expand-row-keys="expands"
      @expand-change="expandSelect" accordion
      :cell-style="{'text-align':'center'}"
      height="100%"
      >
		<el-table-column type="expand">
		  <template slot-scope="scope" >
			<el-form label-position="left" inline class="demo-table-expand">
			  <el-table
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        :header-cell-style="{background:'#eef1f6',color:'#000','text-align':'center'}"
        :cell-style="{'text-align':'center'}"
				:data="tableDataProject"
				:row-class-name="tableRowClassName"
				>
				<el-table-column
				  prop="proName"
				  label="项目名称"
				  width="180">
				</el-table-column>
				<el-table-column
				  prop="memberName"
				  label="项目主持人"
				  width="180">
				</el-table-column>
				<el-table-column
				  prop="proDepartment"
				  label="所属部门"
				  width="180">
				</el-table-column>
				<el-table-column
				  prop="proPhone"
				  label="联系电话"
				  width="180">
				</el-table-column>
				<el-table-column
				  prop="proEmail"
				  label="邮箱">
				</el-table-column>
        <el-table-column
          prop="proApproval"
          :formatter="formatterProApproval"
          label="项目状态">
        </el-table-column>
				<el-table-column fixed="right" width="400" align="center"  label="操作">
					<template slot-scope="scope">
						<el-button type="warning" size="mini" :disabled="scope.row.proApproval!=0 && scope.row.proApproval!=5" @click="modifyProject(scope.$index,scope.row)">修改</el-button>
					  </template>
				</el-table-column>
			  </el-table>
			</el-form>
		  </template>
		</el-table-column>
        <el-table-column prop="batchName" label="批次名称" width="300">
        </el-table-column>
        <el-table-column
          prop="phaseTeacherStart"
          label="开始时间"
          width="120"
        >
        </el-table-column>

        <el-table-column prop="phaseTeacherEnd" label="结束时间" width="120" sortable>
        </el-table-column>
        <el-table-column prop="levelName" label="批次级别"> </el-table-column>
        <el-table-column prop="company" label=" 所属单位"> </el-table-column>
        <el-table-column fixed="right" width="400" align="center">
          <template slot="header">
            <input
              v-model="keywords"
              placeholder="请输入批次名称"
              class="el-input__inner"/>
          </template>

          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleDeclare(scope.$index, scope.row)"
              :disabled="!(new Date(nowDate) >= Date.parse(scope.row.phaseTeacherStart) && new Date(nowDate) <= Date.parse(scope.row.phaseTeacherEnd) )">申报</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="resource-page">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[5, 10, 15]"
          :page-size="pagesize"
          :pager-count="7"
          layout=" prev, pager, next,sizes, jumper"
          :total="filterDataShow.length"
        >
        </el-pagination>
      </div>

  </div>
</template>
<script>
export default {
  data() {
    return {
    Batchloading:true,
    loading:true,
    selectedBatchInfo:{},
	  levelId:'',
	  classify:'',
	  modifyStatus:false,
	  teacherinfo:'',
	  tableDataProject: [], //分页前的数据
	  expands: [],
	  nowDate:"",
    keywords: "", // 搜索的关键字
    filterDataShow: [], //储存符合条件的数据
    tableData: [], //分页前的数据
    pagesize: 10,
    currentPage: 1,
	getRowKeys(row) {
	return row.batchId
	},
    };
  },
  mounted() {
    this.teacherinfo = this.$store.state.info
    this.getDataAxios();
    this.searchResource(); //在页面挂载后调用此方法，确保数据与页面发生了交互
    this.getNowDate();
  },
  methods: {
    formatterProApproval(row,column){
      if(row.proApproval==0){
        return "待提交"
      }
      if(row.proApproval==1){
        return "学院待审核"
      }
      if(row.proApproval==2){
        return "学校待审核"
      }
      if(row.proApproval==3){
        return "专家待审核"
      }
      if(row.proApproval==4 && this.selectedBatchInfo.phaseExpertStart!=null){
        return "学校一审通过"
      }
      if(row.proApproval==4 && this.selectedBatchInfo.phaseExpertStart==null){
        return "学校审核通过"
      }
      if(row.proApproval==5){
        return "驳回待提交"
      }
      if(row.proApproval==6){
        return "学院审核不通过"
      }
      if(row.proApproval==7 && this.selectedBatchInfo.phaseExpertStart!=null){
        return "学校一审不通过"
      }
      if(row.proApproval==7 && this.selectedBatchInfo.phaseExpertStart==null){
        return "学校审核通过"
      }
      if(row.proApproval==8){
        return "学校二审通过"
      }
      if(row.proApproval==9){
        return "学校二审不通过"
      }

    },
	  modifyProject(index,row){
		  this.$router.push({path:'/declareEdit',query:{batch_id:row.batchId,level_id:this.levelId,classify:this.classify,projectId:row.proId}})
		  console.log(row)
	  },
	  tableRowClassName({row, rowIndex}) {
	          if (rowIndex>=0) {
	            return 'warning-row';
	          }
	          return '';
	        },
	  getProjectDataAxios(batch_Id) {
      this.loading=true
      this.tableDataProject=[]
	    let that = this;
	    this.$api.batch.getProjectByBatchId({
	  	batchId: batch_Id
	    }).then(
	      (response)=>{
		  var tempData=response.data.data
		  var result=[]
		  var isfl
		  for(var i = 0 ;i < tempData.length;i++ ){
			  isfl=false;
			  for(var j=0;j< result.length;j++){
				  if(tempData[i].proId == result[j].proId){
					  isfl=true
					  break
				  }
			  }
			  if(!isfl){
				  result.push(tempData[i])
				}}
	  	  this.tableDataProject=result.filter((item)=>{
        return item.belong==this.teacherinfo.gh && item.ranking==1
         })
        this.loading=false
	  	})
	  },
	  expandSelect(row, expandedRows) {
	      var that = this
	      if (expandedRows.length) {
	        that.expands = []
	        if (row) {
	          that.expands.push(row.batchId)// 每次push进去的是每行的ID
	        }
	      } else {
	        that.expands = []// 默认不展开
	      }
        //获得选中批次的信息，用于判断是否有专家审核和学校二审
        this.tableData.forEach(item =>{
            if(item.batchId==row.batchId){
              this.selectedBatchInfo=item
            }
        })
	  	  this.getProjectDataAxios(row.batchId)
        this.classify=row.classifyName
        this.levelId=row.levelId
	    },
		getNowDate(){
		  var nowDate = new Date();
		  var year = nowDate.getFullYear();
		  var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1): nowDate.getMonth() + 1;
		  var day = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate
		   .getDate();
		  var dateStr = year + "-" + month + "-" + day;
		  this.nowDate=dateStr
		},
    newBatch() {
      this.$router.push("/batchSetting");
    },
    //获取批次数据
    getDataAxios() {
     console.log(this.teacherinfo)
     this.$api.batch.getBatchByDepId({
        depId:this.teacherinfo.bmbh
     }).then((response)=>{
		 console.log(response)
     this.tableData=response.data.data
     this.Batchloading=false
		 this.searchResource()
	 })
    },
    //查询
    searchResource() {
      this.currentPage = 1; //将当前页设置为1，确保每次都是从第一页开始搜
      let filterKeywords = this.keywords.trim();
      let filerReasource = this.tableData.filter((item) => {
        //过滤全部数据
        if (item.batchName.includes(filterKeywords)) {
          return item;
        }
      });
      this.filterDataShow = filerReasource; //将符合条件的内容赋给filterDataShow
    },
    handleSizeChange(psize) {
      this.pagesize = psize;
    },
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
    },
    handleDeclare(index, row) {
      this.$api.project.getProjectsByGH({gh:this.teacherinfo.gh}).then((response)=>{
        //获得该教师主持的项目，并过滤掉重复的项目(由于视图的原因，返回会有重复，前端做处理)
        var tempData=response.data.data
        var result=[],tempResult
        var isfl
        for(var i = 0 ;i < tempData.length;i++ ){
          isfl=false;
          for(var j=0;j< result.length;j++){
        	  if(tempData[i].proId == result[j].proId){
        		  isfl=true
        		  break
        	  }
          }
          if(!isfl){
        	  result.push(tempData[i])
        	}}
          //去重后，在获得该批次下的,条件BatchId
          tempResult=result.filter(item=> item.batchId==row.batchId)
          //项目主持数大于最大主持数则给出提示
          if(tempResult.length>=row.maxHost){
            this.$message({
            	message: "该批次最大主持数为"+row.maxHost+",主持数已到最大限至!",
            	type: "warning",
            	offset: screen.height / 2 - 100,
            	center: true
            })
          }else{
            this.$router.push({path:'/declareFill' , query:{batch_id:row.batchId,level_id:row.levelId,classify:row.classifyName}})
          }
      })

      // this.$router.push({path:'/declareFill' , query:{batch_id:row.batchId,level_id:row.levelId,classify:row.classifyName}})
	  // this.$router.push({path:'/declareEdit' , query:{batch_id:row.batchId,level_id:row.levelId,classify:row.classifyName}})
    },
  },
  watch: {
    //监听搜索框内容，当搜索框内容发生变化时调用searchResource方法
    keywords() {
      this.searchResource();
    },
  },
  computed: {
    //showTable计算属性通过slice方法计算表格当前应显示的数据
    showTable() {
      return this.filterDataShow.slice(
        (this.currentPage - 1) * this.pagesize,
        this.currentPage * this.pagesize
      );
    },
  },
};
</script>
<style scoped>
#find {
  width: 400px;
  margin-bottom: 30px;
}


</style>
